﻿Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Net.Mail
Imports System.Net

Public Class EmailManagement
    Private client As SmtpClient
    Private mailMsg As System.Net.Mail.MailMessage

    Private m_subject As String

    Public Property Subject() As String
        Get
            Return m_subject
        End Get
        Set(value As String)
            m_subject = value
        End Set
    End Property


    Private m_toMailAddress As String

    Public Property ToMailAddress() As String
        Get
            Return m_toMailAddress
        End Get
        Set(value As String)
            m_toMailAddress = value
        End Set
    End Property
    Private m_body As String

    Public Property Body() As String
        Get
            Return m_body
        End Get
        Set(value As String)
            m_body = value
        End Set
    End Property


    Private m_port As Integer

    Public Property Port() As Integer
        Get
            Return m_port
        End Get
        Set(value As Integer)
            m_port = value
        End Set
    End Property

    
    Public Sub New(toMailAddress As String, body As String, subject As String)
        Try
            Me.m_toMailAddress = toMailAddress
            Me.m_body = body
            Me.m_port = 587
            Me.m_subject = subject

            emailSet()

        Catch lg As Exception
            'sentMail();
        End Try
    End Sub

    Public Sub sendMail()
        Try
            client.Port = m_port
            client.Send(mailMsg)

        Catch lg As Exception
        End Try
    End Sub

    ''' <summary>
    ''' Sets the email
    ''' </summary>
    Private Sub emailSet()
        Try
            mailMsg = New MailMessage()

            mailMsg.From = New MailAddress("towermalta@gmail.com")

            mailMsg.[To].Add(m_toMailAddress)

            mailMsg.Subject = Subject

            mailMsg.IsBodyHtml = True

            mailMsg.BodyEncoding = Encoding.UTF8

            mailMsg.Body = m_body

            mailMsg.Priority = MailPriority.Normal

            ' Smtp configuration

            client = New SmtpClient("smtp.gmail.com")

            client.UseDefaultCredentials = False


            client.Credentials = New NetworkCredential("towermalta@gmail.com", "towermalta123")

            'client.Host = "smtp.gmail.com";

            client.EnableSsl = True

            'client.Port = 587;
            '25
            Dim userState As Object = mailMsg

        Catch lg As Exception
        End Try

    End Sub

End Class
